package com.dayuanit.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import com.dayuanit.domain.Order;
import com.dayuanit.service.JedisService;

@Service
public class JedisServiceImpl implements JedisService {
	
	private static final Logger logger = LoggerFactory.getLogger(JedisServiceImpl.class);
	
	 @Autowired
	 private RedisTemplate<String, Order> redisTemp;
	 
	 @Resource(name="redisTemplate")
	 private ValueOperations<String, List<Map<String, String>>> provinceTemp;
	 
	 @Resource(name="redisTemplate")
	 private ValueOperations<String, List<Map<String, String>>> cityTemp;
	 
	 @Resource(name="redisTemplate")
	 private ListOperations<String, String> orderQueue;

	@Override
	public List<Map<String, String>> listProvince() {
		List<Map<String, String>> list = Collections.EMPTY_LIST;
		String key ="dayuanit:mall:listprovince";
		boolean exist = redisTemp.hasKey(key);
		if (!exist) {
			return list;
		}
		
		list = provinceTemp.get(key);
		
		return list;
	}

	@Override
	public void saveProvince(List<Map<String, String>> value) {
		String key ="dayuanit:mall:listprovince";
		logger.info("保存省份信息到缓存");
		provinceTemp.set(key, value);
	}

	@Override
	public List<Map<String, String>> listCity(String proCode) {
		String key ="dayuanit:mall:province:" + proCode + ":city";
		List<Map<String, String>> list = Collections.EMPTY_LIST;
		
		boolean exist = redisTemp.hasKey(key);
		if (!exist) {
			return list;
		}
		
		list = cityTemp.get(key);
		
		return list;
	}

	@Override
	public void saveCity(String proCode, List<Map<String, String>> value) {
		String key ="dayuanit:mall:province:" + proCode + ":city";
		logger.info("保存城市信息到缓存");
		cityTemp.set(key, value);
	}

	@Override
	public String popOrderQueue() {
		String key = "dayuanit:pay:order";
		String v = orderQueue.rightPop(key);
		logger.info("=============" + v);
		return v;
	}

	@Override
	public void pushOrderQueue(String value) {
		String key = "dayuanit:pay:order";
		logger.info("支付成功订单再次放入队列");
		orderQueue.leftPush(key, value);
	}

}
